// pages/search/search.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 文本轮播数组
        texts:[
            {text:"很喜欢 很短暂 很遗憾"},
            {text:"Capitol Kids!"},
            {text:"天空有雨 伞下有你"},
            {text:"爱意东升西落"},
            {text:"做个白日梦"},
        ],
        list:[], //页面初始化时的所有歌曲
        successList:[],     //输入完成确定后显示的歌曲列表
        inputing:[],    //正在输入时展示的列表
        hotList:[],  //热搜榜展示数组
        historys:[], //搜索历史记录暂存数组
        arrayname:"search", //搜索成功后歌曲集合名，便于播放页列表展示
        searchshowed:false, //控制的搜索成功后的列表显示/隐藏
        inputshowed:false, //控制的输入框于文本轮播之间的显示/隐藏
        focus:false,        //控制输入框的光标显示/隐藏
        history:false, //控制历史记录的显示/隐藏
        prompt:false, //控制登录提示框的显示/隐藏
        inputvalue:'' //搜索输入框内的值

    },
    //搜索成功后，点击歌曲播放图标是触发，判断当前是否已登录，若未登录则触发登录提示框，已登录则跳转到播放页
    LoginStatus(e){
        console.log(e.currentTarget.dataset.id);
        console.log(e.currentTarget.dataset.arrayname);
        let id = e.currentTarget.dataset.id
        let arrayname = e.currentTarget.dataset.arrayname
        //获取数据缓存的数据集合名
        wx.getStorageInfo({
          success: (res) => {
            console.log(res.keys);
            let a = res.keys.some(value=> value == 'userInfo' )
            console.log(a);
            //判断登录信息是否存在，否则触发登录提示框，要求用户登录
            if(!a){
              console.log("未登录");
              this.setData({prompt:true})
            }else{
                this.playbandler()
              console.log('已登录');
              wx.navigateTo({
                url: '/pages/player/player',
                success(res) {
                  // 通过 eventChannel 向被打开页面传送数据
                  res.eventChannel.emit('acceptDataFromOpenerPage', { id: id ,arrayname: arrayname})
                }
              })
            }
          },
        })
      },
    //点击登录提示框取消按钮时触发
    close(){
        this.setData({
            prompt:false //隐藏登录提示框
        })
    },
    //点击登录提示框登录按钮时触发
    login(){
            let that = this
            wx.getUserProfile({
              desc: '获取您的个人信息',
              success(res){
                  console.log(res);
                  console.log(res.userInfo);
                  wx.setStorage({
                    key:'userInfo',
                    data:res.userInfo
                  })
                  that.setData({
                    status:true,
                    prompt:false,
                    userinfo:res.userInfo
                  })
              }
            })
    },
    //点击文本轮播时触发、进入输入状态
    showinput(){
        this.setData({inputshowed:true,focus:true})
    },
    //点击输入框取消按钮时触发、取消输入状态、展示文本轮播
    hideinput(){
        this.setData({inputshowed:false,inputvalue:''})
    },
    //单击播放图标触发，将搜索的歌曲存入数据缓存
    playbandler(){
        let list = this.data.successList
        console.log(list);
        wx.setStorage({
            key:'searchList',
            data:list
        })
    },
    //当输入框获取焦点时触发
    inputfocus(e){
        this.setData({searchshowed:false})
        //调用inputTyping方法
        this.inputTyping(e)
        
    },
    //单击历史记录信息时触发
    ClickHistory(e){
        console.log(e.currentTarget.dataset.value);
        let item = e.currentTarget.dataset.value
        let list = this.data.list
        let successList = list.filter(value=>value.name.indexOf(item) != -1)
        console.log(successList);
        this.setData({
            successList,
            searchshowed:true,
            inputshowed:true,
            focus:false,
            history:true,
            inputvalue:item,
        })
        let his = this.data.historys
        //加入历史记录时，判断加入数据（不能为空、不能于上一个历史记录相同）
        if (his[0] != item && item!="") {
            his.unshift(item) 
            console.log(his);
            wx.setStorage({
                key:"history",
                data: his,
            })
        }
        let that = this
        wx.getStorage({
            key:'history',
            success(res){
                console.log(res.data);
                that.setData({
                    historys:res.data
                })
            }
        })
    },
    //正在输入时触发
    inputTyping(e){
        console.log(e.detail.value);
        let item = e.detail.value
        let list = this.data.list
        let inputing = list.filter(value=>value.name.indexOf(item) != -1)
        console.log(inputing);
        this.setData({
            inputing,
            searchshowed:false,
            inputvalue:e.detail.value
        })
    },
    //输入完成确定之后触发
    inputsuccess(e){
        console.log(e.detail.value);
        let item = e.detail.value
        let list = this.data.list
        let successList = list.filter(value=>value.name.indexOf(item) != -1)
        console.log(successList);
        this.setData({
            successList,
            searchshowed:true,
            history:true,})
        let his = this.data.historys
        if (his[0] != e.detail.value && e.detail.value!="") {
            his.unshift(e.detail.value) 
            console.log(his);
            wx.setStorage({
                key:"history",
                data: his,
            })
        }
        let that = this
        wx.getStorage({
            key:'history',
            success(res){
                console.log(res.data);
                that.setData({
                    historys:res.data
                })
            }
        })       
    },
    //单击清除历史记录时触发，并将数据缓存中的历史记录、数组（historys）中的数据一并清空，同时隐藏历史记录样式
    clearhandler(){
        wx.removeStorage({
          key: 'history',
          success(res){
              console.log(res);
          }
        })
        this.setData({
            historys:[],
            history:false,

        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //页面初始化加载的数据
        let that = this
        let db = wx.cloud.database()
        db.collection('hot_search_list').get({
            success(res){
                console.log(res);
                that.setData({
                    hotList:res.data[0].hot_search_list
                })
            }
        })
        db.collection('newest').get({
            success(res){
                console.log(res.data[0].arrayitem);
                let list = that.data.list
                list = list.concat(res.data[0].arrayitem)
                console.log(list);
                that.setData({list})
            }
        })
        db.collection('recommend').get({
            success(res){
                console.log(res.data[0].arrayitem);
                let list = that.data.list
                list = list.concat(res.data[0].arrayitem)
                console.log(list);
                that.setData({list})
            }
        })
        db.collection('rotation').get({
            success(res){
                console.log(res.data[0].arrayitem);
                let list = that.data.list
                list = list.concat(res.data[0].arrayitem)
                console.log(list);
                that.setData({list})
            }
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //页面显示时将历史记录展示
        let that = this
        wx.getStorage({
            key: 'history',
            success (res) {
              console.log(res.data)
              if (res.data != "") {
                  that.setData({history:true})
              }
              that.setData({
                  historys:res.data
              })
              console.log(that.data.historys);
            }
          })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})